<?php
 mb_internal_encoding("utf-8");
 require "../document/class_corpus.php";
 require "../document/class_document.php";
 require "../document/class_document_xml.php";

 require "../files/tool_files_dirs.php";

 require "../misc/class_tag.php";
 require "../misc/tool_color.php";

 require "../tree/class_tree.php";
 require "../tree/class_tree_xhtml.php";
 require "../tree/class_structure.php";
 require "../tree/tool_tree.php";

 require "../array/tool_array.php";

 require "../pear_graph/tool_pear_graph.php";

 require "../maths/tool_maths.php";

 require "./tool_string.php";
 require "./class_segment.php";


 $corpus=tool_corpus::get_corpus_regexp("../xml/europa/","f.*xml");
// $corpus=tool_corpus::get_corpus_regexp("../xml/europa/",".fr.xml");
// $corpus=tool_corpus::get_corpus_regexp("../xml/test/",".fr.xml");
 $array_doc=$corpus->__get_array_doc();
// $document=$array_doc[0];
// echo count($array_doc);
//die();

//die();

$factory_mot=new factory_mot();
$res_x=$res_y=array();
foreach($array_doc as $id=>$document){
  $id_doc=$document->__get_id();
  $structure=$document->__get_structure();
  $signature=$structure->__get_signature();
  unset($structure);
  tool_tree::cleanstructure(&$signature);
  $signature->__recalc_idn();
  $test=new RecursiveIteratorIterator($signature,RecursiveIteratorIterator::SELF_FIRST);
  foreach($test as $id_bloc=>$bloc_xhtml){
    $texte=tool_tree::tree_xhtml2texte($bloc_xhtml->__get_value());
    $factory_mot->get_all_mots($texte,$id_doc);
//    echo  "\r".number_format(memory_get_usage(),0,".",",");
  }
//  echo "\n";
}

//    echo  "=>".number_format(memory_get_usage(),0,".",",")."\n";
$factory_mot->init_etiquetage();
//$start=microtime(true);
$factory_mot->init_chunk();
//$end=microtime(true)-$start;
//echo "\n total :: $end";
//    echo  "=>".number_format(memory_get_usage(),0,".",",")."\n";
$all_seg=$factory_mot->__get_array_segment();
//    $r=memory_get_usage()/count($all_seg);
//    echo  "=>".number_format($r,0,".",",")."\n";
//die();
$tool_graph=new Tool_pear_graph();

//$repartition_chunk=$factory_mot->__get_repartiton_chunk();
$repartition_segment=$factory_mot->__get_repartition_segment();
//print_r($repartition_segment);
//die();
$res_v=$res_all=$res_p=$res_i=$zipf=array();
foreach($repartition_segment as $id_doc=>$array_bloc){
  foreach($array_bloc as $id_bloc=>$array_id_segment){
    foreach($array_id_segment as $id_segment){
      $mot=$factory_mot->__get_word($id_segment);
      $len=$mot->__get_len();
      $type=$mot->__get_attribute("type");
      if($type=="v") $res_v[$len]++;
      if($type=="p") $res_p[$len]++;
      if($type=="i") $res_i[$len]++;
      $id_doc=mb_strcut($id_doc,-6,2);
      $res_all[$id_doc][$len]++;
      $zipf[$id_doc][$mot->__tostring()]++;
    }
  }
}

$len_chunk=array();
$repartition_chunk=$factory_mot->__get_repartition_chunk();
//print_r($repartition_chunk);
//die();
foreach($repartition_chunk as $id_doc=>$array_bloc){
  $id_doc=mb_strcut($id_doc,-6,2);
  foreach($array_bloc as $id_bloc=>$array_chunk){
    foreach($array_chunk as $array_id_segment){
      $len=0; 
      foreach($array_id_segment as $id_segment){
        $mot=$factory_mot->__get_word($id_segment);
        /*if($mot->__get_attribute("type")!="v")*/$len+=$mot->__get_len();
      }
      $len_chunk[$id_doc][$len]++;
    }
  }
}
//die();

/*
foreach($zipf as $id_doc => $res_zipf){
  $res_zipf=tool_maths::zipf($res_zipf);
  $tool_graph->__add_array_x(array_keys($res_zipf));
  $tool_graph->__add_array_y(array_values($res_zipf));
  $tool_graph->__add_plot_title("zipf log*log : $id_doc");
}
*/
/*
foreach($len_chunk as $id_doc => $len){
  if($id_doc=="fr"){
    ksort($len);
    $tool_graph->__add_array_x(array_keys($len));
    $tool_graph->__add_array_y(array_values($len));
    $tool_graph->__add_plot_title("len_chunk : $id_doc");
  }
}
*/

foreach($res_all as $id_doc => $len_word){
//  if($id_doc=="fi"){
    ksort($len_word);
    $tool_graph->__add_array_x(array_keys($len_word));
    $tool_graph->__add_array_y(array_values($len_word));
    $tool_graph->__add_plot_title("len_word : $id_doc");
//  }
}

//die();
//print_r($zipf_log);
//die();
//print_r($zipf);
//die();
//$tool_graph->__add_array_x(array_keys($res_all));
//$tool_graph->__add_array_y(array_values($res_all));
//$tool_graph->__add_array_x(array_keys($zipf));
//$tool_graph->__add_array_y(array_values($zipf));
//$tool_graph->__add_array_x(array_keys($zipf_log_fi));
//$tool_graph->__add_array_y(array_values($zipf_log_fi));
//$tool_graph->__add_plot_title("zipf finnois log*log");
//$tool_graph->__add_plot_title("zipf");


/*
$tool_graph->__add_array_x(array_keys($res_v));
$tool_graph->__add_array_x(array_keys($res_i));
$tool_graph->__add_array_x(array_keys($res_p));
*/
/*
$tool_graph->__add_array_y(array_values($res_v));
$tool_graph->__add_array_y(array_values($res_i));
$tool_graph->__add_array_y(array_values($res_p));
*/
//$graph=$tool_graph->plot03();
//$graph=$tool_graph->plot_log_log01();
$graph=$tool_graph->plot03();
ob_start();
$graph->done();
$graph_svg=ob_get_contents();
Tool_files::file_write("./out.svg",$graph_svg);
ob_clean();
?>
